package com.hitqz.robot.driver.netty.router;

import com.hitqz.robot.driver.netty.strategy.Track2BStrategy;
import com.hitqz.robot.driver.netty.strategy.Track64Strategy;
import com.hitqz.robot.driver.netty.strategy.Track65Strategy;
import com.hitqz.robot.driver.netty.strategy.TrackMessageHandlerStrategy;
import org.aspectj.bridge.MessageHandler;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TrackMessageRouter {
    // 路由表，存放命令和对应的handler
    private static final Map<String, TrackMessageHandlerStrategy> strategyMap = new ConcurrentHashMap<>();

    static {
        // 初始化路由表，将命令和处理器绑定
        // 字母大写
        strategyMap.put(getHexKey((byte)0x2b), new Track2BStrategy());
        strategyMap.put(getHexKey((byte)0x64), new Track64Strategy());
        strategyMap.put(getHexKey((byte)0x65), new Track65Strategy());
    }

    // 根据命令获取处理器
    public static TrackMessageHandlerStrategy getHandlerStrategy(byte mainCmd) {
        return strategyMap.get(getHexKey(mainCmd));
    }


    private static String getHexKey(Byte value){
        // 转换为无符号整数，避免负数问题
        int unsignedByte = value & 0xFF;
        String hex = Integer.toHexString(unsignedByte).toUpperCase();  // 转换为大写的十六进制
        // 补齐到两位
        if (hex.length() == 1) {
            hex = "0" + hex;
        }
        return hex;
    }


}
